//
//  String + subcript.swift
//  Edaixi-iOS
//
//  Created by 刘树华 on 2017/11/28.
//  Copyright © 2017年 刘树华. All rights reserved.
//

import Foundation

extension String {
    
    var lenght : Int {
        if self.isEmpty {
            return 0
        }else{
           return self.count
        }
    }
    subscript(r: Range<Int>) -> String {
        get {
            assert(r.upperBound < self.lenght, "字符串越界")
            let startIndex = self.index(self.startIndex, offsetBy: r.lowerBound)
            
            let endIndex =  self.index(self.startIndex, offsetBy: r.upperBound)
            return String(self[startIndex...endIndex])
        }
    }
    func substring(startIndex: Int, length: Int) -> String {
        assert(startIndex + length <= self.lenght, "字符串越界")
        let start = self.index(self.startIndex, offsetBy: startIndex)
        
        let end =  self.index(self.startIndex, offsetBy: startIndex + length)
        return String(self[start..<end])
    }
}
